package routes

import (
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/core/router"
	"github.com/kataras/iris/v12/mvc"
	"micro-grpc-server/app/common"
	"micro-grpc-server/app/http/controllers/admin"
	"micro-grpc-server/app/http/controllers/api"
	"micro-grpc-server/app/http/controllers/web"
	"micro-grpc-server/app/http/middleware"
	"micro-grpc-server/services/music"
	"micro-grpc-server/services/user"
	"micro-grpc-server/services/wechat"
)

func Register(app *iris.Application) {
	app.UseRouter(middleware.CrsAuth())
	app.UseRouter(common.SessManager.Handler())
	app.Use(middleware.AuthLogin.LoginByToken)

	app.SetExecutionRules(iris.ExecutionRules{
		Done: iris.ExecutionOptions{Force: true},
	})

	// 微信自动回复
	mvc.New(app.Party("/wechat")).Register(wechat.NewWechatClient()).Handle(new(web.WechatController))

	var sessManager = common.SessManager.Start
	// api路由注册
	app.PartyFunc("api/", func(route router.Party) {
		// 注册文档
		mvc.New(route.Party("doc")).Handle(new(api.DocController))
		// api路由
		route.PartyFunc("grpc/", func(r router.Party) {
			// 一些额外的接口
			mvc.New(r).Register(user.NewUserClient(), sessManager).Handle(new(api.DefaultController))
			mvc.New(r.Party("user")).Register(user.NewUserClient(), sessManager).Handle(new(api.UserController))
			mvc.New(r.Party("music")).Register(music.NewMusicClient(), sessManager).Handle(new(api.MusicController))
			// 管理员路由注册
			r.PartyFunc("admin/", func(adminRoute router.Party) {
				mvc.New(adminRoute.Party("wechat")).Register(wechat.NewWechatClient(), sessManager).Handle(new(admin.WechatController))
				mvc.New(adminRoute.Party("user")).Register(user.NewUserClient(), sessManager).Handle(new(admin.UserController))
			})
		})
	})
}
